/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.basicmediadecoder;


import android.animation.Animator;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.airbnb.lottie.LottieAnimationView;
import com.example.android.common.media.MediaCodecWrapper1;
import com.example.android.utils.KCacheUtils;

import java.io.File;
import java.io.IOException;

/**
 * This activity uses a {@link android.view.TextureView} to render the frames of a video decoded using
 * {@link android.media.MediaCodec} API.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Animator.AnimatorListener {

    // A utility that wraps up the underlying input and output buffer processing operations
    // into an east to use API.

    Button btnH5;
    Button btnH10;
    Button btnH15;


//    WebView mWebView;

    LinearLayout parent;
    int i = 1;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);
        KCacheUtils.init(getApplicationContext());
        parent = findViewById(R.id.parent);

        btnH5 = findViewById(R.id.btnH5);
        btnH10 = findViewById(R.id.btnH10);
        btnH15 = findViewById(R.id.btnH15);

//        mWebView = findViewById(R.id.webview);

        btnH5.setOnClickListener(this);
        btnH10.setOnClickListener(this);
        btnH15.setOnClickListener(this);
        startWebView();
//        lottieAnimationView();
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                lottieAnimationView();
//            }
//        },2000);

    }

    private void startWebView() {
//        WebSettings settings = mWebView.getSettings();
//        settings.setJavaScriptEnabled(true);
//        settings.setDefaultTextEncodingName("GBK");
//        mWebView.setBackgroundColor(0);
//        mWebView.loadUrl("file:///android_asset/lottie/images.html");
    }

//    private void lottieAnimationView() {
//
//
//        LottieAnimationView lottieAnimationView = findViewById(R.id.animation_view1);
//        lottieAnimationView.setImageAssetsFolder("aassistant");
//        lottieAnimationView.setAnimation("assistant_1.json");
//        lottieAnimationView.setRepeatCount(2);
//        lottieAnimationView.addAnimatorListener(this);
//        lottieAnimationView.playAnimation();
//    }

    @Override
    protected void onDestroy() {
        LottieAnimationView lottieAnimationView = findViewById(R.id.animation_view1);
        lottieAnimationView.cancelAnimation();
        super.onDestroy();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public void test(View v) {
        startActivity(new Intent(this, TestActivity.class));
    }

    public void testAll(View v) {
        String s = Environment.getExternalStorageDirectory().getAbsolutePath() + "/audioScore/source/";
        final File[] files = new File(s).listFiles();
        new Thread(new Runnable() {
            @Override
            public void run() {
                int count = 0;
                for (File path : files) {
                    try {
                        Uri audioUri = Uri.fromFile(path);
                        MediaCodecWrapper1 mCodecWrapper1 = MediaCodecWrapper1.fromAudioUri(MainActivity.this, audioUri);
                        System.out.println("file:" + path);

                        if (mCodecWrapper1 != null) {
                            mCodecWrapper1.startDecode();
                            count ++;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("size = " + files.length + "  count:" + count);
            }
        }).start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnH5:
                startPlayback(R.raw.h5);
                break;
            case R.id.btnH10:
                startPlayback(R.raw.h10);
                break;
            case R.id.btnH15:
                startPlayback(R.raw.h15);
                break;

        }
    }

    public void startPlayback(int rawId) {

        // Construct a URI that points to the video resource that we want to play
        Uri audioUri = Uri.parse("android.resource://"
                + getPackageName() + "/"
                + rawId);

        try {

            MediaCodecWrapper1 mCodecWrapper1 = MediaCodecWrapper1.fromAudioUri(this, audioUri);
            mCodecWrapper1.startDecode();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAnimationStart(Animator animation) {

    }

    @Override
    public void onAnimationEnd(Animator animation) {
        i++;
        if (i > 4) {
            i = 1;
        }
        final int id = i % 2 + 1;
        String name = "assistant_" + i + ".json";
        LottieAnimationView lottieAnimationView = findViewById(getResources().getIdentifier("animation_view" + id, "id", getPackageName()));
        lottieAnimationView.setImageAssetsFolder("aassistant");
        lottieAnimationView.setAnimation(name);
        lottieAnimationView.setRepeatCount(2);
        lottieAnimationView.addAnimatorListener(this);
        lottieAnimationView.playAnimation();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideView(id);
            }
        }, 50);
//        hideView(i);

//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                String name = "assistant_" + i + ".json";
//                LottieDrawable drawable = new LottieDrawable();
//                lottieAnimationView.setImageAssetsFolder("aassistant");
//                lottieAnimationView.setImageDrawable(drawable);
//                lottieAnimationView.setAnimation(name);
//                lottieAnimationView.setRepeatCount(3);
//                lottieAnimationView.setScale(0.5f);
//                lottieAnimationView.setProgress(0);
//                lottieAnimationView.playAnimation();
//            }
//        },50);
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    private void hideView(int i) {
//        for (int j = 1; j < 5; j++) {
//            if (j != i) {
        int j = i - 1;
        if (j <= 0) {
            j = 2;
        }
        LottieAnimationView lottieAnimationView = findViewById(getResources().getIdentifier("animation_view" + j, "id", getPackageName()));
        lottieAnimationView.setImageDrawable(null);
//            }
    }
}
